package cn.hy.face.thrift;


import cn.hy.face.common.vo.PageInfo;
import cn.hy.face.thrift.dto.reponese.BusiGradeDTO;
import cn.hy.face.thrift.dto.reponese.SysOfficeDTO;
import cn.hy.face.thrift.dto.reponese.SysUserDTO;
import cn.hy.face.thrift.dto.request.OfficeQueryDTO;
import cn.hy.pms.thrift.SysUserThrift;
import cn.hy.pms.thrift.utils.ThriftUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import org.apache.thrift.TException;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * @program: face
 * @author: qingxiangen
 * @create: 2019-08-17 18:21
 * @description: 外部接口管理类 - 服务
 **/
@Service
public class ThriftService {

    /**
     * 根据学校id获取年级信息
     * @return
     */
    public List<BusiGradeDTO> findGradeBySchoolId(String schoolId) throws TException {
        String jsonResult = ThriftUtils.findGradeBySchoolId(schoolId);
        List<BusiGradeDTO> gradeList = JSONObject.parseArray(jsonResult, BusiGradeDTO.class);
        return gradeList;
    }
    
    /**
     * 根据学校id,json参数查询班级信息
     * @return
     */
    public List<SysOfficeDTO> findOfficeBySchoolIdAndOtherParams(String schoolId, OfficeQueryDTO queryDTO) throws TException {
        String jsonResult = ThriftUtils.findOfficeBySchoolIdAndOtherJsonParams(schoolId, JSON.toJSONString(queryDTO));
        List<SysOfficeDTO>  officeList = JSONObject.parseArray(jsonResult, SysOfficeDTO.class);
        return officeList;
    }

    /**
     * 根据学校id,json参数查询班级信息
     * @return
     */
    public List<SysOfficeDTO> findClassUserDetailBySchoolIdAndClassIdAndOtherParams(String schoolId, String classId) throws TException {
        String jsonResult = ThriftUtils.findClassUserDetailBySchoolIdAndClassIdAndOtherParams(schoolId, classId, null,null);
        List<SysOfficeDTO> officeList = JSONObject.parseArray(jsonResult, SysOfficeDTO.class);
        return officeList;
    }

    /**
     * 根据学校id,json参数查询班级信息
     * @return
     */
    public PageInfo<SysUserDTO> findUsersByOfficeId(String schoolId, String classId, Integer pageNum, Integer pageSize,  String realName) throws TException {
        if(pageNum == null){
            pageNum = 1;
        }
        if(pageSize == null){
            pageSize = 500;
        }
        String jsonResult = ThriftUtils.findPageUser(pageNum, pageSize, schoolId, classId, "3",null,  realName);
        PageInfo<SysUserDTO> userPageInfo = JSONObject.parseObject(jsonResult, new TypeReference<PageInfo<SysUserDTO>>() {});
        return userPageInfo;
    }

    /**
     * 根据用户id查询用户信息
     * @return
     */
    public SysUserDTO findUserByUserId(String userId) throws TException {
        SysUserThrift sysUserThrift = ThriftUtils.findUserByUserId(userId);
        if (sysUserThrift == null){
            return null;
        }
        SysUserDTO sysUser = new SysUserDTO();
        sysUser.setId(sysUserThrift.getId());
        sysUser.setAvatar(sysUserThrift.getAvatar());
        sysUser.setRealName(sysUserThrift.getRealName());
        sysUser.setStatus(sysUserThrift.getStatus());
        return sysUser;
    }
}
